package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketChannelMapper;
import com.cskaoyan.market.db.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketOrderMapper;
import com.cskaoyan.market.service.AdminOrderService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;
    @Autowired
    MarketOrderMapper orderMapper;
    @Autowired
    MarketChannelMapper channelMapper;


    @Override
    public Map<String, Object> detail(Integer id) {
        Map<String,Object> objectMap=new HashMap<>();
        // orderGoods
        MarketOrderGoodsExample marketOrderGoodsExample=new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria1 = marketOrderGoodsExample.createCriteria();
        criteria1.andOrderIdEqualTo(id);
        // criteria1.andIdEqualTo(Integer.valueOf(id));
        List<MarketOrderGoods> marketOrderGoods=orderGoodsMapper.selectByExample(marketOrderGoodsExample);
        objectMap.put("orderGoods",marketOrderGoods);

        // order
        MarketOrderExample marketOrderExample=new MarketOrderExample();
        MarketOrderExample.Criteria criteria2=marketOrderExample.createCriteria();
        criteria2.andIdEqualTo(id);

        List<MarketOrder> marketOrders=orderMapper.selectByExample(marketOrderExample);
        objectMap.put("order",marketOrders.get(0));

        return objectMap;
    }

    @Override
    @Transactional
    public int refund(Integer orderId, Integer refundMoney) {
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        criteria.andIdEqualTo(orderId);
        List<MarketOrder> marketOrders = orderMapper.selectByExample(marketOrderExample);
        marketOrders.get(0).setOrderStatus((short) 203);
        int i = orderMapper.updateByPrimaryKey(marketOrders.get(0));
        return i;
    }

    @Override
    public List<MarketOrder> list(Integer page, Integer limit, String[] orderStatusArray, String sort, String order, String start, String end, String orderSn, Integer userId) {
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        if (orderStatusArray!=null&&orderStatusArray.length!=0){
            Short[] orderStatusArray1=new Short[orderStatusArray.length];
            for (int i = 0; i < orderStatusArray1.length; i++) {
                orderStatusArray1[i]= Short.valueOf(orderStatusArray[i]);
            }
            List<Short> orderStatusArray2=new ArrayList<>(Arrays.asList(orderStatusArray1));
            criteria.andOrderStatusIn(orderStatusArray2);
        }

        if(!StringUtils.isEmpty(start)&&!StringUtils.isEmpty(end)){
            LocalDateTime startTime= LocalDateTime.parse(start.replace(" ","T"));
            LocalDateTime endTime = LocalDateTime.parse(end.replace(" ","T"));
            criteria.andAddTimeGreaterThan(startTime);
            criteria.andEndTimeLessThan(endTime);
        }

        if(!StringUtils.isEmpty(orderSn)){
            criteria.andOrderSnLike("%"+orderSn+"%");
        }
        if(userId!=null){
            criteria.andUserIdEqualTo(userId);
        }
        marketOrderExample.setOrderByClause(sort+" "+order);
        PageHelper.startPage(page,limit);
        List<MarketOrder> marketOrders = orderMapper.selectByExample(marketOrderExample);
        return marketOrders;
    }

    @Override
    public MarketChannel[] channel() {
        MarketChannel[] marketChannels= channelMapper.selectAll().toArray(new MarketChannel[0]);
        return marketChannels;
    }

    @Override
    @Transactional
    public int ship(Integer orderId, String shipChannel, String shipSn) {
        MarketOrder order=orderMapper.selectByPrimaryKey(orderId);
        order.setShipChannel(shipChannel);
        order.setShipSn(shipSn);
        order.setShipTime(LocalDateTime.now());
        order.setOrderStatus((short) 301);
        int i = orderMapper.updateByPrimaryKey(order);
        return i;
    }

    @Override
    @Transactional
    public int delete(Integer orderId) {
        // 设置逻辑删除
        MarketOrderExample orderExample=new MarketOrderExample();
        /*orderExample.or().andDeletedEqualTo(false);
        MarketOrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andIdEqualTo(orderId);*/
        orderExample.or().andIdEqualTo(orderId);
        List<MarketOrder> marketOrders = orderMapper.selectByExample(orderExample);
        int i=623;
        if(marketOrders.get(0).getOrderStatus()==203||marketOrders.get(0).getOrderStatus()==401||marketOrders.get(0).getOrderStatus()==402){
            i=orderMapper.logicalDeleteByExample(orderExample);
        }

        return i;
    }
}
